package com.jbh.wanandroidkotlin.activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import com.jbh.apilibrary.AppData
import com.jbh.apilibrary.bean.BaseResponse
import com.jbh.apilibrary.bean.LoginResultBean
import com.jbh.apilibrary.i.ApiCallBack
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.manager.AppActivityManager
import com.jbh.wanandroidkotlin.manager.UserInfoManager
import kotlinx.android.synthetic.main.activity_register.*
import com.jbh.wanandroidkotlin.util.closeInput


class RegisterActivity : BaseActivity(),View.OnClickListener {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        btn_register.setOnClickListener (RegisterActivity@this)
    }



    /**
     *
     * 注册账号
     *
     * @param userName
     *
     * @param userPwd
     *
     */
    private fun registerTask(userName:String,userPwd:String){
        AppData.instance().registerTask(userName,userPwd,userPwd, object : ApiCallBack<BaseResponse<LoginResultBean>> {
            override fun success(data: BaseResponse<LoginResultBean>) {
                println("[registerTask] - > success = ${Gson().toJson(data)}")
                runOnUiThread{
                    UserInfoManager.newInstance().saveInfo(data.data)
                    AppActivityManager.instance.exit()
                    goToActivity(MainActivity::class.java)
                }
            }
            override fun error(error: String) {
                println("[registerTask] - > error = = $error")
                runOnUiThread{
                    Toast.makeText(this@RegisterActivity,error, Toast.LENGTH_LONG).show()
                }
            }
        })
    }



    override fun onClick(p0: View?) {
        when(p0!!.id){
            R.id.btn_register ->{
                closeInput(RegisterActivity@this)
                registerTask(username.text.toString(),userPassword.text.toString())
            }
        }
    }
}